package cn.com.hhrcw.service.impl;

import cn.com.hhrcw.entity.PiInfo;
import cn.com.hhrcw.entity.SysUser;
import cn.com.hhrcw.mapper.MySysUserMapper;
import cn.com.hhrcw.mapper.PiInfoMapper;
import cn.com.hhrcw.service.IPiInfoService;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 个人基本信息 服务实现类
 * </p>
 *
 * @author jiangdong
 * @since 2020-10-30
 */
@Service
public class PiInfoServiceImpl extends ServiceImpl<PiInfoMapper, PiInfo> implements IPiInfoService {

    @Autowired
    private MySysUserMapper sysUserMapper;

    private static Logger logger = LoggerFactory.getLogger(PiInfoServiceImpl.class);

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createUserInfo(SysUser user, PiInfo info) {
        sysUserMapper.insert(user);
        info.setUserId(user.getId());
        this.baseMapper.insert(info);
        return true;
    }

    @Override
    public PiInfo getById(Serializable id) {
        LambdaQueryWrapper<PiInfo> piInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        piInfoLambdaQueryWrapper.eq(PiInfo::getUserId, id);
        return this.baseMapper.selectOne(piInfoLambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean trans(String current, String next) {
        this.lambdaUpdate().eq(PiInfo::getUserId, current).set(PiInfo::getEnRoleType, 0).update();
        this.lambdaUpdate().eq(PiInfo::getId, next).set(PiInfo::getEnRoleType, 1).update();
        return true;
    }

    @Override
    public IPage<PiInfo> selectJoinUserPage(LambdaQueryWrapper wrapper, IPage page) {
        return this.baseMapper.selectJoinUserPage(page, wrapper);
    }


    /**
        date:yyyy-mm-dd格式
     */
    @Override
    public List<PiInfo> getByDate(Date beginDate,Date endDate){
        LambdaQueryWrapper<PiInfo> piInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        piInfoLambdaQueryWrapper.between(PiInfo::getCreateTime,beginDate,endDate).or().between(PiInfo::getUpdateTime,beginDate,endDate);
        return this.baseMapper.selectList(piInfoLambdaQueryWrapper);
    }
}
